<?php
// +----------------------------------------------------------------------
// | XiaMiCMF - 代码如诗
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://shujuwajue.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小虾米 <509129@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;

use Common\Lib\Think\Page;
use Common\Controller\AdminBaseController;

class RouteController extends AdminBaseController
{
    const CNAME = '路由规则';
    const   URL_KEY = 'url_settings';

    protected $route_model;
    protected $setting_model;

    public function _initialize() {
        parent::_initialize();
        $this->route_model = D('Common/Route');
        $this->setting_model = D("Common/Setting");
        $this->assign('TITLE', self::CNAME);
    }

    /**
     * 路由列表
     */
    public function index() {

        if (IS_POST) {
            $post = array_map('trim', I('post.'));
            $data = array(
                'key'   => self::URL_KEY,
                'value' => json_encode($post)
            );
            if ($this->setting_model->save($data) !== false) {
                x_set_sys_config(array(
                    'URL_MODEL'       => $post['url_model'],
                    'URL_HTML_SUFFIX' => $post['html_suffix']
                ));
                $this->success('设置成功');
            } else {
                $this->error('设置失败');
            }
        } else {

            x_get_routes(false);

            $setting = $this->setting_model->getSettingByKey(self::URL_KEY);
            $this->assign($setting);


            $where = array();
            $count = $this->route_model->where($where)->count();
            $page = new Page($count, C('PAGE_LIST_ROWS'));
            $show = $page->show();
            $list = $this->route_model->where($where)->limit($page->firstRow . ',' . $page->listRows)->select();

            $this->assign('list', $list); // 赋值数据集
            $this->assign('page', $show); // 赋值分页输出
            $this->assign('count', $count);
            $this->display(); // 输出模板
        }

    }

    /**
     * 添加路由规则
     */
    public function add() {
        if (IS_POST) {
            $data = $this->route_model->create();
            if ($data) {
                $id = $this->route_model->add($data);
                if ($id) {
                    x_get_routes(true); //刷新配置文件
                    $this->success('添加成功');
                } else {
                    $this->error('添加失败');
                }
            } else {
                $this->error($this->ad_model->getError());
            }
        } else {
            $this->display();
        }
    }

    /**
     * 编辑路由规则
     */
    public function edit() {
        if (IS_POST) {
            $data = $this->route_model->create();
            if ($data) {
                if ($this->route_model->save($data) !== false) {
                    x_get_routes(true); //刷新配置文件
                    $this->success('编辑成功');
                } else {
                    $this->error('编辑失败:'.$this->route_model->getError());
                }
            } else {
                $this->error($this->route_model->getError());
            }
        } else {
            $route_id  = I('get.route_id', 0, 'intval');
            $detail = $this->route_model->find($route_id);
            $this->assign('detail', $detail);
            $this->display();
        }
    }

    /**
     * 删除路由规则
     */
    public function delete() {
        $route_id = I('post.route_id',0,'intval');

        if (is_numeric($route_id)) {
            x_get_routes(true); //刷新配置文件
            $this->route_model->delete($route_id);
            $this->success('删除成功', U('index'));
        } elseif (is_array($route_id)) {
            x_get_routes(true); //刷新配置文件
            $this->route_model->delete(implode(',', $route_id));
            $this->success('删除成功', U('index'));
        } else {
            $this->error('请选择要删除的' . self::CNAME);
        }
    }

    /**
     * 更新菜单排序
     */
    public function updateSort() {
        $status = parent::_updateListSort($this->route_model);
        if ($status) {
            $this->success("更新排序成功", U('index'));
        } else {
            $this->error("更新排序失败");
        }
    }

    /**
     * 显示或禁用路由规则
     */
    public function changeStatus() {

        $route_id = I('post.route_id', 0, 'intval');

        if (!empty($route_id)) {

            if (!$detail = $this->route_model->find($route_id)) {
                $this->error('请选择要改变状态的' . self::CNAME);
            }
            $data['route_id']  = $route_id;
            $data['status'] = $detail['status'] ? 0 : 1 ;
            if ($this->route_model->save($data) !== false) {
                x_get_routes(true); //刷新配置文件
                $this->success(self::CNAME . '状态改变成功', U('index'));
            } else {
                $this->error(self::CNAME . '状态改变失败');
            }

        } else {
            $this->error('请选择要改变状态的' . self::CNAME);
        }
    }
}